package Forum;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import login.UserModel;
import notification.LogNotifModel;
import notification.NotifManager;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import Candidate.CandidateForm;
import Eclass.EclassForm;
import Eclass.BatchClass.BatchClassManager;
import Eclass.Schedule.ScheduleForm;
import Eclass.Score.ScoreForm;
import Master.SchoolForm;

public class ForumThreadAction extends Action {
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			
		ForumThreadForm fForm = (ForumThreadForm) form;
		ForumThreadManager fManager = new ForumThreadManager();
		HttpSession session = request.getSession(true);
		UserModel user = (UserModel)session.getAttribute("userLogin");
		
		//-------------------------------------------------------------- Navigation ----------------------------------------------------------
				CandidateForm candidateForm = new CandidateForm();
				SchoolForm schoolForm = new SchoolForm();
				EclassForm eClassForm = new EclassForm();
				//ForumThreadForm forumForm = new ForumThreadForm();
				ScoreForm scoreForm = new ScoreForm();
				
				if("updateCandidate".equalsIgnoreCase(fForm.getTask())) {
					candidateForm.getcModel().setRegisNo("01-13-12- 029");
					candidateForm.setTask("update");
					return mapping.findForward("candidate");
				}else if("manageTrainee".equalsIgnoreCase(fForm.getTask())) {	
					candidateForm.setTask("manageTrainee");
					return mapping.findForward("candidate");
				}else if("batch".equalsIgnoreCase(fForm.getTask())) {
					eClassForm.setTask("batch");
					return mapping.findForward("eclass");
				}else if("listExamPrep".equalsIgnoreCase(fForm.getTask())) {
					eClassForm.setTask("listExamPrep");
					return mapping.findForward("eclass");
				}else if("manageSchool".equalsIgnoreCase(fForm.getTask())) {
					schoolForm.setTask("manageSchool");
					return mapping.findForward("master");
//		  		}else if("forumPage".equalsIgnoreCase(fForm.getTask())) {
//					forumForm.setTask("forumPage");
//					return mapping.findForward("forum");
				}else if("report".equalsIgnoreCase(fForm.getTask())) {
					scoreForm.setTask("report");
					return mapping.findForward("score");
				}
		//------------------------------------------------------------------------------------------------------------------------------
		
//---------------------------------------------------Manage Forum----------------------------------------------------------------------
		if("insert".equalsIgnoreCase(fForm.getTask())) {
			fForm.setListBatch(fManager.selectBatch());
			fForm.setListClass(fManager.selectClass());
			fForm.setTask("add");
			return mapping.findForward("actionForum");
		}
		if("add".equalsIgnoreCase(fForm.getTask())) {
			boolean result = fManager.addForum(fForm.getfModel());
			
			//Rere 17 Des 2013
			this.setResult(result, "Add new forum", session);
		}
		if("edit".equalsIgnoreCase(fForm.getTask())) {
			fForm.setListBatch(fManager.selectBatch());
			fForm.setListClass(fManager.selectClass());
			fForm.setfModel(fManager.selectUpdate(fForm.getIdUpdate()));
			fForm.setTask("update");
			return mapping.findForward("actionForum");
		}
		if("update".equalsIgnoreCase(fForm.getTask())) {
			boolean result = fManager.updateForum(fForm.getfModel());
			
			//Rere 17 Des 2013
			this.setResult(result, "Update forum", session);
		}
		
//------------------------------------------------------------------Thread-------------------------------------------------------------
		if("forumPage".equalsIgnoreCase(fForm.getTask())){
			//System.out.println("Masuk Forum Page.... ");
			fForm.setListForumThread(fManager.selectThread(user.getUserid().toString(),user.getRole().toString()));
			return mapping.findForward("forumPage");
		}
		
		
		ForumThreadForm forumNotifForm = (ForumThreadForm) session.getAttribute("showThreadFromNotif");
		session.removeAttribute("showThreadFromNotif");
		if( forumNotifForm !=null && forumNotifForm.getTask()!=null && "thread".equalsIgnoreCase(forumNotifForm.getTask()) ){
			try{
				fForm.setTask(forumNotifForm.getTask());
				fForm.setIdThread(forumNotifForm.getIdThread());
			}
			catch(Exception ex){
				ex.printStackTrace();
			}
		}
		
		if("thread".equalsIgnoreCase(fForm.getTask())) {
			List a = new ArrayList();
			List b = new ArrayList();
			a = fManager.selectComment(fForm.getIdThread());
			System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-================== size thread: "+ a.size());
			
			for (int i = 0; i < a.size(); i++) {
				ForumCommentModel fcm = new ForumCommentModel();
				fcm = (ForumCommentModel) a.get(i);
				String com = "";
				com = fcm.getPt1().toString();
				if(fcm.getPt2() == null) {
				} else {
					com += fcm.getPt2().toString();
				}
				if(fcm.getPt3() == null) {
				} else {
					com += fcm.getPt3().toString();
				}
				if(fcm.getPt4() == null) {
				} else {
					com += fcm.getPt4().toString();
				}
				if(fcm.getPt5() == null) {
				} else {
					com += fcm.getPt5().toString();
				}
				fcm.setComments(com);
				b.add(fcm);
			}
			
//			fForm.setListComments(fManager.selectComment(fForm.getIdThread()));
			fForm.setListComments(b);
			//Rere 17 Des 2013
			fForm.setJudulThread(((ForumCommentModel)fManager.selectComment(fForm.getIdThread()).get(0)).getThreadTitle());
			
			return mapping.findForward("thread");
		}
		
//------------------------------------------------------------------Comment-------------------------------------------------------------
		if("editPost".equalsIgnoreCase(fForm.getTask())){
			fForm.setThreadId(fForm.getIdThread());
			fForm.setTask("updatePost");
			fForm.setIdSelect(fForm.getIdEdit());
			
			ForumCommentModel fcm = new ForumCommentModel();
			fcm = fManager.selectUpdateComment(fForm.getIdThread());
			String com = "";
			com = fcm.getPt1().toString();
			
			if(fcm.getPt2() == null) {
			} else {
				com += fcm.getPt2().toString();
			}
			if(fcm.getPt3() == null) {
			} else {
				com += fcm.getPt3().toString();
			}
			if(fcm.getPt4() == null) {
			} else {
				com += fcm.getPt4().toString();
			}
			if(fcm.getPt5() == null) {
			} else {
				com += fcm.getPt5().toString();
			}
			fcm.setComments(com);
			
			fForm.setfComModel(fcm);
			//fForm.setfComModel(fManager.selectUpdateComment(fForm.getIdThread()));
			
			//Rere 17 Des 2013
			fForm.setJudulThread(((ForumCommentModel)fManager.selectComment(fForm.getIdSelect()).get(0)).getThreadTitle());
			
			return mapping.findForward("editComment");
		}
		if("updatePost".equalsIgnoreCase(fForm.getTask())) {
			boolean result = fManager.updateComment(fForm.getfComModel());
			
			List a = new ArrayList();
			List b = new ArrayList();
			a = fManager.selectComment(fForm.getIdSelect());
			System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-================== size thread: "+ a.size());
			
			for (int i = 0; i < a.size(); i++) {
				ForumCommentModel fcm = new ForumCommentModel();
				fcm = (ForumCommentModel) a.get(i);
				String com = "";
				com = fcm.getPt1().toString();
				if(fcm.getPt2() == null) {
				} else {
					com += fcm.getPt2().toString();
				}
				if(fcm.getPt3() == null) {
				} else {
					com += fcm.getPt3().toString();
				}
				if(fcm.getPt4() == null) {
				} else {
					com += fcm.getPt4().toString();
				}
				if(fcm.getPt5() == null) {
				} else {
					com += fcm.getPt5().toString();
				}
				fcm.setComments(com);
				b.add(fcm);
			}
			fForm.setListComments(b);
			//fForm.setListComments(fManager.selectComment(fForm.getIdSelect()));
			
			//Rere 17 Des 2013
			fForm.setJudulThread(((ForumCommentModel)fManager.selectComment(fForm.getIdSelect()).get(0)).getThreadTitle());
			this.setResult(result, "Update comment post", session);
			
			return mapping.findForward("thread");
		}
		if("cancelPost".equalsIgnoreCase(fForm.getTask())) {
			List a = new ArrayList();
			List b = new ArrayList();
			a = fManager.selectComment(fForm.getIdSelect());
			System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-================== size thread: "+ a.size());
			
			for (int i = 0; i < a.size(); i++) {
				ForumCommentModel fcm = new ForumCommentModel();
				fcm = (ForumCommentModel) a.get(i);
				String com = "";
				com = fcm.getPt1().toString();
				if(fcm.getPt2() == null) {
				} else {
					com += fcm.getPt2().toString();
				}
				if(fcm.getPt3() == null) {
				} else {
					com += fcm.getPt3().toString();
				}
				if(fcm.getPt4() == null) {
				} else {
					com += fcm.getPt4().toString();
				}
				if(fcm.getPt5() == null) {
				} else {
					com += fcm.getPt5().toString();
				}
				fcm.setComments(com);
				b.add(fcm);
			}
			fForm.setListComments(b);
			//fForm.setListComments(fManager.selectComment(fForm.getIdSelect()));
			
			//Rere 17 Des 2013
			fForm.setJudulThread(((ForumCommentModel)fManager.selectComment(fForm.getIdSelect()).get(0)).getThreadTitle());
						
			return mapping.findForward("thread");
		}
		if("addComment".equalsIgnoreCase(fForm.getTask())) {			
			fForm.setThreadId(fForm.getIdThread());
			fForm.setTask("insertComment");
			
			//Rere 17 Des 2013
			fForm.setIdSelect(fForm.getIdThread());
			fForm.setJudulThread(((ForumCommentModel)fManager.selectComment(fForm.getIdThread()).get(0)).getThreadTitle());
			
			return mapping.findForward("editComment");
		}		
		if("insertComment".equalsIgnoreCase(fForm.getTask())) {
			boolean result = fManager.addComment(fForm.getfComModel());
			if(user!=null) System.out.println("User Not NULLLLL!!!! - "+user.getUserid());
			if(insertNotifComment(fForm.getfComModel(),fManager,user)){
				System.out.println("Success Insert New Notification Comment");
			}
			else{
				System.out.println("Failed to Insert New Notification Comment");
			}
			
			List a = new ArrayList();
			List b = new ArrayList();
			a = fManager.selectComment(fForm.getThreadId());
			System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-================== size thread: "+ a.size());
			
			for (int i = 0; i < a.size(); i++) {
				ForumCommentModel fcm = new ForumCommentModel();
				fcm = (ForumCommentModel) a.get(i);
				String com = "";
				com = fcm.getPt1().toString();
				if(fcm.getPt2() == null) {
				} else {
					com += fcm.getPt2().toString();
				}
				if(fcm.getPt3() == null) {
				} else {
					com += fcm.getPt3().toString();
				}
				if(fcm.getPt4() == null) {
				} else {
					com += fcm.getPt4().toString();
				}
				if(fcm.getPt5() == null) {
				} else {
					com += fcm.getPt5().toString();
				}
				fcm.setComments(com);
				b.add(fcm);
			}
			fForm.setListComments(b);
			//fForm.setListComments(fManager.selectComment(fForm.getThreadId()));
			
			//Rere 17 Des 2013
			fForm.setJudulThread(((ForumCommentModel)fManager.selectComment(fForm.getThreadId()).get(0)).getThreadTitle());
			this.setResult(result, "Post new comment", session);
			
			return mapping.findForward("thread");
		}
//------------------------------------------------------------------Search-------------------------------------------------------------
		if("search".equalsIgnoreCase(fForm.getTask())) {
			fForm.setListForumThread(fManager.selectSearch(user.getUserid().toString(), user.getRole().toString(), fForm.getSearchKeyword().toString()));
			return mapping.findForward("forumPage");
		}
//-----------------------------------------------------------------default-------------------------------------------------------------
		if("question".equalsIgnoreCase(fForm.getTask())) {
			return mapping.findForward("question");
		}
		
		if("backToListForum".equalsIgnoreCase(fForm.getTask())){
			fForm.setListForumThread(fManager.selectAll());
			return mapping.findForward("manageForum");
		}
		fForm.setListForumThread(fManager.selectAll());
		return mapping.findForward("manageForum");
	}
	
//----------------------------------------------------------- set result --------------------------------------------------	
	//Rere 17 Des 2013
	public void setResult(boolean result, String action, HttpSession session){
		String resultText = "";
		if(result){
			resultText = action + " succeed.";
		}else{
			resultText = action + " failed.";
		}
		session.setAttribute("result", resultText);
	}
	
	private Boolean insertNotifComment(ForumCommentModel fcm,ForumThreadManager fManager, UserModel user){
		ForumThreadModel ftm = fManager.selectUpdate(fcm.getForumThreadId().toString());
		
		LogNotifModel lgm = new LogNotifModel();
		lgm.setUserId(fcm.getUserId());
		lgm.setForumThreadId(fcm.getForumThreadId());
		lgm.setDescription(user.getUserid().toUpperCase() + " commented @ Thread "+ftm.getThreadTitle());
		lgm.setBatchId(ftm.getBatchId());
		lgm.setClassId(ftm.getClassId());
		lgm.setLink("/WebFinalProject/Forum.do?task=showThread&threadId="+fcm.getForumThreadId());
		NotifManager notifMgr = new NotifManager();
		Boolean cek = false;
		if(notifMgr.insertLogNotifForum(lgm)){
			if(notifMgr.insertNotifForum()) {
				cek = true;
			}
		}
		return cek;
	}
}
